﻿using Furion.Schedule;

namespace Jobs;

/// <summary>
/// 结束拼团
/// </summary>
public class EndTeamBuy : IJob
{
    private readonly ISqlSugarClient db;

    private readonly Logics.Teambuy teamLogic;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="db"></param>
    /// <param name="teamLogic"></param>
    public EndTeamBuy(ISqlSugarClient db, Logics.Teambuy teamLogic)
    {
        this.db = db;
        this.teamLogic = teamLogic;
    }

    /// <summary>
    /// 执行结束拼团
    /// </summary>
    /// <param name="context"></param>
    /// <param name="stoppingToken"></param>
    /// <returns></returns>
    public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
    {
        var teams = await db.Queryable<Entity.TeamBuy>().Where(x => x.MaxNum == x.CurrentNum && x.State == Entity.TeamBuyState.进行中).ToListAsync();

        foreach (var team in teams) await teamLogic.End(team.Id);
    }
}
